package com.lagou.servlet;

import com.lagou.bena.Student;
import com.lagou.service.AdminServiceImp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "deleteStuServlet", urlPatterns = "/deleteStu")
public class deleteStuServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置编码格式
        request.setCharacterEncoding("utf-8");
        // 从前端页面获取用户需要删除的学号
        Integer id = Integer.parseInt(request.getParameter("stuID"));
        AdminServiceImp asi = new AdminServiceImp();
        // 获取对数据库操作的执行结果
        int delRes = asi.deleteStuService(id);
        if (delRes > 0) {
            // 添加学生信息成功，跳转到学生列表
            // 查询所有学生信息，得到list集合
            List<Student> students = asi.showAllService();

            // 将查询出的所有学生信息集合存入session对象中实现多个请求共享
            request.getSession().setAttribute("students", students);
            request.getSession().setAttribute("delRes", "success");

            // 实现客户端的跳转
            response.sendRedirect("stuList.jsp");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
